package com.xdf.wxbebarrel.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xdf.wxbebarrel.model.WechatMenuButton;
import com.xdf.wxbebarrel.model.WechatSubButton;
import com.xdf.wxbebarrel.model.WechatViewButton;
import com.xdf.wxbebarrel.model.WxAccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URLEncoder;

@Component
public class CreatMenu {
    @Autowired
    private WxAccessToken wxAccessToken;
    private static final ObjectMapper mapper = new ObjectMapper();

    public void create() throws IOException {
        WechatMenuButton button = new WechatMenuButton();
        // 本日明细
        String todayDetail = "https://www.barrel.fit/wxfebarrel/barreloneday.html";
        String todayDetailEncoded = URLEncoder.encode(todayDetail);
        String todayDetailFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
                DevMaterial.JZY.APPID +
                "&redirect_uri=" + todayDetailEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

        // 超时查询
        String barrelovertime = "https://www.barrel.fit/wxfebarrel/barrelovertime.html";
        String barrelovertimeEncoded = URLEncoder.encode(barrelovertime);
        String barrelovertimeFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
                DevMaterial.JZY.APPID +
                "&redirect_uri=" + barrelovertimeEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

        // 网筒操作
        String barreloperate = "https://www.barrel.fit/wxfebarrel/barreloperate.html";
        String barreloperateEncoded = URLEncoder.encode(barreloperate);
        String barreloperateFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
                DevMaterial.JZY.APPID +
                "&redirect_uri=" + barreloperateEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";


        // 查询 - 仓位查询
        String storagequery = "https://www.barrel.fit/wxfebarrel/storagequery.html";
        String storagequeryEncoded = URLEncoder.encode(storagequery);
        String storagequeryFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
                DevMaterial.JZY.APPID +
                "&redirect_uri=" + storagequeryEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

        // 查询 - 网筒查询
        String barrelquery = "https://www.barrel.fit/wxfebarrel/barrelquery.html";
        String barrelqueryEncoded = URLEncoder.encode(barrelquery);
        String barrelqueryFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
                DevMaterial.JZY.APPID +
                "&redirect_uri=" + barrelqueryEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

        // 关于
        String admin = "https://www.barrel.fit/wxfebarrel/admin.html";
        String adminEncoded = URLEncoder.encode(admin);
        String adminEncodedFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
                DevMaterial.JZY.APPID +
                "&redirect_uri=" + adminEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

        // 测试
        String test = "https://www.barrel.fit/wxfebarrel/test.html";
        String testEncoded = URLEncoder.encode(test);
        String testEncodedFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
                DevMaterial.JZY.APPID +
                "&redirect_uri=" + testEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

        button.getButton().add(new WechatViewButton("网筒操作", barreloperateFull));

        WechatSubButton sb02 = new WechatSubButton("查询");
        sb02.getSub_button().add(new WechatViewButton("仓位查询", storagequeryFull));
        sb02.getSub_button().add(new WechatViewButton("网筒查询", barrelqueryFull));
        sb02.getSub_button().add(new WechatViewButton("超时查询", barrelovertimeFull));
        sb02.getSub_button().add(new WechatViewButton("本日明细", todayDetailFull));
        button.getButton().add(sb02);

        WechatSubButton sb03 = new WechatSubButton("关于");
        sb03.getSub_button().add(new WechatViewButton("管理员", adminEncodedFull));
        sb03.getSub_button().add(new WechatViewButton("测试", testEncodedFull));
        button.getButton().add(sb03);

        String btnString = mapper.writeValueAsString(button);

        String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
        String access_token = wxAccessToken.getAccessToken();
        url = url.replace("ACCESS_TOKEN", access_token);
        String result = HttpUtils.postForm(url, btnString,false);
        System.out.println("创建菜单的结果：" + result);
    }

}
